package com.example.spzx.Service.impl;


import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.example.exception.OmegaException;
import com.example.spzx.Mapper.SysUserMapper;
import com.example.spzx.Mapper.SysUserRoleMapper;
import com.example.spzx.Service.SysUserService;
import com.example.spzx.model.dto.system.AssginRoleDto;
import com.example.spzx.model.dto.system.LoginDto;
import com.example.spzx.model.dto.system.SysUserDto;
import com.example.spzx.model.entity.system.SysRoleUser;
import com.example.spzx.model.entity.system.SysUser;
import com.example.spzx.model.vo.common.Result;
import com.example.spzx.model.vo.common.ResultCodeEnum;
import com.example.spzx.model.vo.system.LoginVo;
import com.example.utils.AuthContextUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class SysUserServiceImpl implements SysUserService {
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private SysUserRoleMapper sysUserRoleMapper;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    public LoginVo login(LoginDto loginDto) {
        String captcha = loginDto.getCaptcha();
        String codekey = loginDto.getCodeKey();
        String redisCode = stringRedisTemplate.opsForValue().get("user:login:validatecode:" + codekey);
        System.out.println(redisCode + codekey);
        if (StrUtil.isEmpty(redisCode) || !StrUtil.equalsIgnoreCase(redisCode, captcha)) {
            throw new OmegaException(ResultCodeEnum.VALIDATECODE_ERROR);
        }
        stringRedisTemplate.delete("user:login:validatecode:" + codekey);
        SysUser sysUser = sysUserMapper.selectByUserName(loginDto.getUserName());
        if (sysUser == null) {
            throw new OmegaException(ResultCodeEnum.LOGIN_ERROR);
//            throw new RuntimeException("用户名密码错误");
        }
        String inputPassword = loginDto.getPassword();
        String md5InputPassword = DigestUtils.md5DigestAsHex(inputPassword.getBytes());
        if (!md5InputPassword.equals(sysUser.getPassword())) {
            throw new OmegaException(ResultCodeEnum.LOGIN_ERROR);
        }
        AuthContextUtil.set(sysUser);
        String token = UUID.randomUUID().toString().replace("-", "");
        stringRedisTemplate.opsForValue().set("user:login" + token, JSON.toJSONString(sysUser), 30, TimeUnit.MINUTES);

        LoginVo loginVo = new LoginVo();
        loginVo.setToken(token);
        loginVo.setRefresh_token("");
        return loginVo;

    }

    public SysUser getUserInfo(String token) {
        String UserJson = stringRedisTemplate.opsForValue().get("user:login" + token);

        return JSON.parseObject(UserJson, SysUser.class);
    }

    public void logout(String token) {
        stringRedisTemplate.delete("user:login");
        stringRedisTemplate.delete("user:login" + token);
    }

    public PageInfo<SysUser> list(SysUserDto sysUserDto, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<SysUser> page = sysUserMapper.getlist(sysUserDto);
        PageInfo<SysUser> pageInfo = new PageInfo<>();
        pageInfo.setList(page.getResult());
        pageInfo.setTotal(page.getTotal());
        return pageInfo;
    }

    public void add(SysUser sysUser) {
        SysUser sysUser1 = sysUserMapper.selectByUserName(sysUser.getUserName());
        if (sysUser1 != null) {
            throw new OmegaException(ResultCodeEnum.USER_NAME_IS_EXISTS);
        }
        String password = sysUser.getPassword();
        String digestPassword = DigestUtils.md5DigestAsHex(password.getBytes());
        sysUser.setPassword(digestPassword);
        sysUser.setStatus(1);
        sysUserMapper.add(sysUser);

    }

    public void update(SysUser sysUser) {
        SysUser sysUser1=sysUserMapper.selectById(sysUser.getId());
    sysUser1.setUserName(sysUser.getUserName());
    sysUser1.setAvatar(sysUser.getAvatar());
    sysUser1.setPassword(null);
    sysUser1.setDescription(sysUser.getDescription());
    sysUser1.setPhone(sysUser.getPhone());
    sysUser1.setName(sysUser.getName());
    sysUserMapper.update(sysUser1);

    }
    public     void delete(Integer id){
        SysUser sysUser=sysUserMapper.selectById(Long.valueOf(id));
        if(sysUser == null){
            throw new OmegaException(ResultCodeEnum.NO_ERROR);
        }
        sysUserMapper.delete(id);

    }
    public List<Integer> selectrole(Integer id){
        List<Integer> list=sysUserRoleMapper.getbyid(id);
        return list;
    }
    public void setrole(AssginRoleDto assginRoleDto){
     sysUserRoleMapper.deletebyuserid(assginRoleDto.getUserId());
     List<Long> list=assginRoleDto.getRoleIdList();
     for (Long id:list){
         sysUserRoleMapper.insert(assginRoleDto.getUserId(),id);
     }

    }
}
